package com.example.emb.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.emb.entity.Stuworktable;
import com.example.emb.entity.Worktable;
import com.example.emb.entity.Yeartable;
import com.example.emb.mapper.StuworktableMapper;
import com.example.emb.mapper.WorktableMapper;
import com.example.emb.mapper.YeartableMapper;
import com.example.emb.service.common.CosSimilarity;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.example.emb.service.common.CosSimilarity.*;

import java.time.LocalDate;
import java.util.*;

@Component
public class checkTask {
    @Autowired
    WorktableMapper worktableMapper;
    @Autowired
    StuworktableMapper stuworktableMapper;
    @Autowired
    CosSimilarity cosSimilarity;
    @Autowired
    YeartableMapper yeartableMapper;

    /**
     * 执行的时间逻辑是
     * 每天的0点扫一遍work表，看看有没有到ddl的作业任务（workId），如果有就触发下一步，如果没有那么结束
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void DailyCheckWorkTask() {
        Map<Integer, Double> WorkIdRate = new HashMap<>();
        //转换date格式，去扫描endTime
        Date date = new Date();
        java.sql.Date sqlDate = new java.sql.Date(date.getTime());
        //获得所有到截止时间的作业
        List<Worktable> list = worktableMapper.selectByEndTimeToday(sqlDate);
        for (Worktable worktable : list) {
            //表示作业到现在已经结束了，不能再提交了
            worktable.setIsFinal(1);
            worktableMapper.updateById(worktable);
        }

        //先需要一个方法达成获得所有任务的Map<>的需求，返回值是Map<WorkId,List<Map<Id(具体作业的id),Map<word,Frequency>>>>的格式
        //元素之间互不干扰，同时没有外部调用使用异步加快速度
        Map<Integer, List<Map<CharSequence, Integer>>> map = new HashMap<>();
        list.parallelStream().forEach(item -> {
            //先查到对应的work的doc内容
            QueryWrapper<Stuworktable> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("workid", item.getId());
            //不管了，没有doc文件了,同时当他是自动保存了
            List<Stuworktable> list1 = stuworktableMapper.selectList(queryWrapper);
            List<String> list2 = new ArrayList<>();
            list1.forEach(item1 -> {
                list2.add(item1.getWorkDoc());
            });
            List<Map<CharSequence,Integer>> ListMap = cosSimilarity.getAllTermFrequencyMap(list2);
            //list循环是有顺序的
            map.put(item.getId(), ListMap);
        });

        //开始查重,因为map不能异步操作，所以使用多线程的方式编写代码，每一个WorkId启动一个线程
        map.entrySet().parallelStream().forEach(entry->{
            int WorkId=entry.getKey();
            List<Map<CharSequence,Integer>> ListMap=entry.getValue();
            getSingleRate(WorkId,ListMap);
        });

        //表示作业已经查重完成了
        for (Worktable worktable : list) {
            worktable.setIsFinished(1);
        }
    }

    //单一一个作业任务的查重
    private void getSingleRate(int WorkId,List<Map<CharSequence, Integer>> ListMap){
        QueryWrapper<Stuworktable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workid", WorkId);

        //
        List<Stuworktable> list1 = stuworktableMapper.selectList(queryWrapper);
        for (Stuworktable stu:list1){
            Double rate= cosSimilarity.calculateCosineSimilarity(stu.getWorkDoc(),ListMap);
            //将计算得到的rate写回数据库
            stu.setRepeatRate(rate);
            stuworktableMapper.updateById(stu);
        }
    }

    //每年的1.1执行任务，让year更新
    @Scheduled(cron = "0 0 0 1 1 ?")
    public void AddYearTask(){
        List<Integer> list=yeartableMapper.selectAllYears();
        LocalDate localDate = LocalDate.now();
        int yearNow=localDate.getYear();
        if(list.get(0)<yearNow){
            Yeartable yeartable=new Yeartable();
            yeartable.setYears(yearNow);
            yeartableMapper.insert(yeartable);
        }
    }


}
